package com.fastCms.business.frame.utils.qweather;

import com.alibaba.fastjson.JSON;
import com.fastCms.business.frame.utils.HttpclientUtils;
import com.fastCms.business.frame.utils.opc.DateUtil;
import com.fastCms.business.frame.utils.qweather.response.QAirInfoResp;
import com.fastCms.business.frame.utils.qweather.response.QWeatherResp;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class QweatherUtil {
    private final static String AIR_INFO = "https://devapi.qweather.com/v7/air/now?";
    private final static String WEATHER_INFO = "https://devapi.qweather.com/v7/weather/now?";
    private final static String WEATHER_HIS_INFO = "https://devapi.qweather.com/v7/historical/weather?";

    public static QAirInfoResp airInfo(String key, String location) {
        StringBuilder url = new StringBuilder(AIR_INFO);
        url.append("location=");
        url.append(location);
        url.append("&key=");
        url.append(key);
        try {
            log.debug("请求的URL" + url);
            String response = HttpclientUtils.get(url.toString());
            if (response.contains("200")) {
                QAirInfoResp weatherInfoResp = JSON.parseObject(response, QAirInfoResp.class);
                return weatherInfoResp;
            }
        } catch (Exception e) {
            log.error("airInfo {}", e.getMessage());
        }
        return null;
    }

    public static QWeatherResp weatherInfo(String key, String location) {
        StringBuilder url = new StringBuilder(WEATHER_INFO);
        url.append("location=");
        url.append(location);
        url.append("&key=");
        url.append(key);
        try {
            log.info("请求的URL" + url);
            String response = HttpclientUtils.get(url.toString());
            if (response.contains("200")) {
                QWeatherResp weatherInfoResp = JSON.parseObject(response, QWeatherResp.class);
                return weatherInfoResp;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static QWeatherResp weatherHisInfo(String key, String location) {
        StringBuilder url = new StringBuilder(WEATHER_HIS_INFO);
        url.append("location=");
        url.append(location);
        url.append("&key=");
        url.append(key);
        url.append("&date=");
        url.append(DateUtil.getPassDay(9));

        try {
            log.info("请求的URL" + url);
            String response = HttpclientUtils.get(url.toString());
            if (response.contains("200")) {
                QWeatherResp weatherInfoResp = JSON.parseObject(response, QWeatherResp.class);
                return weatherInfoResp;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        QWeatherResp result = weatherHisInfo("c64ca83cc6c044479dbd9ee9068768ec", "101100713");

//        QAirInfoResp result = airInfo("c64ca83cc6c044479dbd9ee9068768ec", "101121501");
        System.out.printf("result= " + result.toString());
    }

}
